// @author A0080413U
package tasky.tests;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.ArrayList;

import org.junit.Test;

import tasky.commons.Task;
import tasky.storage.DesktopPath;
import tasky.storage.Storage;

public class StorageTest {
	

	@Test
	public void testConstructor() throws ClassNotFoundException, IOException {
		Storage st = Storage.getInstance();
		
		assertNotNull (st.getTaskFile());
		assertNotNull (st.getRecoveryFile());
		assertTrue (st.getTaskFile().canRead());
		assertTrue (st.getRecoveryFile().canRead());
		assertTrue (st.getTaskFile().canWrite());
	}

	@Test
	public void testRecovery() throws ClassNotFoundException, IOException {
		Storage st = Storage.getInstance();
		ArrayList<Task> testList = new ArrayList<Task>();
		st.writeToRecovery(testList);

		assertTrue (st.getRecoveryFile().canRead());
		assertEquals(2, st.getRecoveryFile().length());
		
		Task task1 = new Task("cs2103 project","cs2103");
		testList.add(task1);
		Task task2 = task1;
		task2.setDone(true);
		task2.setPriority(true);
		testList.add(task2);
		st.writeToRecovery(testList);
		
		assertTrue(st.getRecoveryFile().length()>2);	
		
	}

	@Test
	public void testStorage() throws ClassNotFoundException, IOException {
		Storage st = Storage.getInstance();
		st.writeToTasks();
		assertEquals("saved", st.writeToStorage(st.getTasks()));
		st.writeToRecovery(st.getTasks());
		assertEquals(st.getRecoveryFile().length(), st.getTaskFile().length());

	}
	
	@Test
	public void testDesktopPath() {
		String path = DesktopPath.getCurrentUserDesktopPath();
		String path2 = DesktopPath.getCurrentUserDesktopPath();
		assertNotNull(path);
		assertNotNull(path2);
		assertEquals(path, path2);
	}
	
	@Test
	public void testExportTaskFile() throws IOException, ClassNotFoundException {
		Storage st = Storage.getInstance();
		assertTrue(st.exportTaskFile("testtaskfile.txt"));
		assertTrue(st.exportTaskFile(""));
	}
	
	@Test
	public void testHelp() throws IOException, ClassNotFoundException {
		Storage st = Storage.getInstance();
		assertNotNull(st.getHelp());
		assertTrue(st.getHelp().length() > 0);

	}
	
}